package www.houdoudou.com.supplier;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import www.houdoudou.com.common.CommonMathFunction;
import www.houdoudou.com.common.ConnFactory;
import www.houdoudou.com.common.TelnumVerifyManagement;

public class OrderManagement {

	 public static JSONObject  manage(String jsonData){
	  	   
	   	  JSONObject result = new JSONObject();
	   		JSONObject jsonfrom= JSONObject.fromObject(jsonData);				
	   		//判断请求的功能d
	   		int FunctionCode=jsonfrom.getInt("FunctionCode");
	   		   PreparedStatement pstm=null; 
	   		   Statement  stm=null;
	   		   ResultSet rs=null; 	
	   		   String CurrentConnUser=CommonMathFunction.genRandomNum(10);
	   		   Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
	   		   switch(FunctionCode){
	   		   
	   		  //查询
	           case 1:{
				
				// step2 数据库输入。
				 
				try {
					   int Machine_ID=jsonfrom.getInt("Machine_ID");
						
					   String sql = "select * from Tb_Machine where Machine_ID="+Machine_ID+"";  
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
						rs= stm.executeQuery(sql);
						if(rs.next()){	
							
							
						}
				}catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "查询失败");
				}finally{
					ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
				}
	           }//
	   		   
	   		   
	   		   //修改
	           case 2:{
				
				// step2 数据库输入。
				 
				try {	// step2 
					   String Customer_Telnum=jsonfrom.getString("Customer_Telnum");					  
					   String sql = "select * from Tb_Customer where Customer_Telnum="+Customer_Telnum+"";   
						stm=conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_UPDATABLE);
						rs= stm.executeQuery(sql);
						if(rs.next()){		
							 JSONObject innerJSONOVerify=jsonfrom;						 
							 innerJSONOVerify.put("Telnum", jsonfrom.getString("Customer_Telnum"));
							if( TelnumVerifyManagement.checkVerifyCode(innerJSONOVerify.toString())){
								rs.updateString("Customer_Secret", jsonfrom.getString("Customer_Secret"));	
								rs.updateRow();
								result.put("ActionStatus", true);
								result.put("ErrorInfo", "修改密码成功");
							}else{
								result.put("ActionStatus", false);
								result.put("ErrorInfo", "验证码不正确");
							}
							
						}else{
							result.put("ActionStatus", false);
							result.put("ErrorInfo", "用户不存在");
						}
				}catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					result.put("ActionStatus", false);
					result.put("ErrorInfo", "数据库异常");
				}finally{
					ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
				}
	          break; }//
	   		   //新增
	   		  case 4:{ 
	   			  
	   			  
	   			             
				   try {		String sql1="insert  into Tb_Order ";
				                String sql2=" (Order_State,Order_Secret,Order_Money,Box_Num,Product_Num,Customer_Telnum,Machine_ID,"
				                		+ "Machine_Address,Machine_Name,Supplier_Telnum,Supplier_Name,Service_Telnum,Arrive_DateTime,"
				                		+ "Placed_DateTime,Arrived_DateTime,Taked_DateTime,Refused_DateTime,Arrive_EarliestTime,"
				                		+ "Take_LatestTime,Retrieve_LatestTime,Refund_ID) ";
				                String sql3=" (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
				        	// String sql = "insert  into Tb_Order (Customer_Telnum,Customer_Secret) values (?,?)";
				                String sql=sql1+sql2+" values "+sql3;
				                pstm=conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
								pstm.setString(1, jsonfrom.getString("Order_State"));
								pstm.setString(2,  getTakeSecret(jsonfrom.getInt("Machine_ID")));	
								pstm.setDouble(3, jsonfrom.getDouble("Order_Money"));
								pstm.setInt(4, jsonfrom.getInt("Box_Num"));	
								pstm.setInt(5, jsonfrom.getInt("Product_Num"));
								pstm.setString(6, jsonfrom.getString("Customer_Telnum"));	
								pstm.setInt(7, jsonfrom.getInt("Machine_ID"));
								
								pstm.setString(8, jsonfrom.getString("Machine_Address"));	
								pstm.setString(9, jsonfrom.getString("Machine_Name"));
								pstm.setString(10, jsonfrom.getString("Supplier_Telnum"));	
								pstm.setString(11, jsonfrom.getString("Supplier_Name"));
								pstm.setString(12, jsonfrom.getString("Service_Telnum"));	
								pstm.setString(13, jsonfrom.getString("Arrive_DateTime"));
								
								pstm.setString(14, "0000-00-00-00:00");	//Placed_DateTime
								pstm.setString(15, "0000-00-00-00:00"); //Arrived_DateTime
								pstm.setString(16, "0000-00-00-00:00");	//Taked_DateTime
								pstm.setString(17, "0000-00-00-00:00"); //Refused_DateTime
								pstm.setString(18, jsonfrom.getString("Arrive_EarliestTime"));
								
								pstm.setString(19, jsonfrom.getString("Take_LatestTime"));
								pstm.setString(20, jsonfrom.getString("Retrieve_LatestTime"));	
								pstm.setInt(20, 0); //Refund_ID
							    pstm.executeUpdate();						    
							   
							    
							    rs=pstm.getGeneratedKeys();
								if(rs.next()){
									int Product_ID= rs.getInt(1);
								result.put("Product_ID",Product_ID);
								}
								 result.put("ActionStatus", true);
				  }catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						result.put("ActionStatus", false);
						result.put("ErrorInfo", "查询失败");
					}finally{
						ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs);
					}
				   
			                 break;} //end of case 4
	   		  
	   		   }// end of switch functionCode
	   		   
	   		   
	   		   return result; 
	   		   
	   		   
	   		
	   		   
		 }
		
	   private static String getTakeSecret(int Machine_ID){
		   String PreparedSecret="";
		   PreparedStatement pstm=null; 
   		   Statement  stm=null;
   		   ResultSet rs=null; 	
   		   String CurrentConnUser=CommonMathFunction.genRandomNum(10);
   		   Connection conn=ConnFactory.getCurrentConnection(CurrentConnUser);
   		   //未激活的订单，密码可能被重写，激活时，需要重新获取密码。
		   try{ String sql="select * from  Tb_Product where Machine_ID ="+Machine_ID+" ";
		   stm=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs= stm.executeQuery(sql);
			boolean isPreparedSecretOK=true;
			  do{
				  PreparedSecret= CommonMathFunction.genRandomNum(6);
				  isPreparedSecretOK=true;
			  rs.first();
			  while(rs.next()){
				int  Order_State=rs.getInt("Order_State");
				boolean isExistedSecretValid=(Order_State>0&&Order_State<6);
				       if(isExistedSecretValid){
				    	   if(rs.getString("Order_Secret").equals(PreparedSecret)){
				    		   isPreparedSecretOK=false;
				    		   break;
				    	   }
				       }
			  }
				   
				   
			   }while(!isPreparedSecretOK);
			   
		   }catch(Exception e){
			   
		   }finally{
				ConnFactory.closeConn(CurrentConnUser, pstm, stm, rs); 
		   }
			   
			   return PreparedSecret;
		   }
	
}
